package StackDemo;


class TestDemo{
    public static void main(String[] args) {
       ListStack ls = new ListStack();
       ls.push(1);
       ls.push(2);
       ls.push(3);
       ls.push(4);
       ls.push(5);

        try {
            System.out.println(ls.pop());
            System.out.println(ls.pop());
            System.out.println(ls.pop());
            System.out.println(ls.pop());
            System.out.println(ls.pop());
            System.out.println(ls.pop());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}

public class ListStack {

    LikedList head = new LikedList(-1);

    public void push(int no){
        LikedList node = new LikedList(no);
        LikedList cur = head;

        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    public int pop(){
        LikedList cur = head;
        if(cur.next == null){
            throw new StackEmptyException("栈空");
        }

        while(cur.next.next != null){
            cur = cur.next;
        }
        int value = cur.next.no;
        cur.next = null;
        return value;
    }
}

// 节点类,定义为外部类
class LikedList{
    int no;
    LikedList next;

    public LikedList(int no){
        this.no = no;
    }
}